//
//  YXYSelectView.m
//  GanJie
//
//  Created by XQ on 2017/10/7.
//  Copyright © 2017年 yxy. All rights reserved.
//

#import "YXYSelectView.h"

@implementation YXYSelectView{
    UIView * _contentView;
    NSString * _selectedStr;
    NSArray * _dataSource;
}

+ (instancetype)initWithDataSource:(NSArray *)dataSource Completion:(SelectBlock)completion{
    
    YXYSelectView * select = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds dataSource:dataSource completion:completion];
    return select;
}

- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource completion:(SelectBlock)block{
    if (self = [super initWithFrame:frame]) {
        self.block = block;
        _dataSource = dataSource;
        [self setupView];
    }
    return self;
}

- (void)setupView{
    
    _selectedStr = _dataSource[0];
    UIView * view = [[UIView alloc] initWithFrame:self.bounds];
    view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    [self addSubview:view];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self addGestureRecognizer:tap];
    
    UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(0, GJScreenH, GJScreenW, 240)];
    contentView.backgroundColor = [UIColor whiteColor];
    _contentView = contentView;
    [self addSubview:contentView];
    
    UIPickerView * pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, GJScreenW, 200)];
    pickView.delegate = self;
    pickView.dataSource = self;
    [contentView addSubview:pickView];
    
    UIButton * cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 50, 40)];
    [cancelBtn setTitle:REAPPString(@"取消") forState:UIControlStateNormal];
    [cancelBtn setTitleColor:COLOR_SUBTITLE forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancelBtn];
    
    UIButton * confrmBtn = [[UIButton alloc] initWithFrame:CGRectMake(GJScreenW - 60, 0, 50, 40)];
    [confrmBtn setTitleColor:COLOR_MAIN forState:UIControlStateNormal];
    [confrmBtn setTitle:@"确认" forState:UIControlStateNormal];
    [confrmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:confrmBtn];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:.3 animations:^{
        contentView.frame = CGRectMake(0, GJScreenH - 240, GJScreenW, 240);
    }];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _dataSource.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _dataSource[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    _selectedStr = _dataSource[row];
}

- (void)cancel{
    [self dismiss];
}

- (void)confirm{
    if (self.block && _selectedStr) {
        self.block(_selectedStr);
    }
    [self dismiss];
}

- (void)dismiss{
    [UIView animateWithDuration:.3 animations:^{
        _contentView.frame = CGRectMake(0, GJScreenH, GJScreenW, 240);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


@end
